package org.jmc.refmanager.domain.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.springframework.orm.jpa.JpaCallback;

public class MaxResultsJpaCallback<E> implements JpaCallback<List<E>> {
	String sqlQuery;
	int maxResults;

	public MaxResultsJpaCallback(String query, int maxResults) {
		this.sqlQuery = query;
		this.maxResults = maxResults;
	}

	@SuppressWarnings("unchecked")
	public List<E> doInJpa(EntityManager em) throws PersistenceException {
		Query query = em.createQuery(sqlQuery);
		query.setFirstResult(0);
		query.setMaxResults(maxResults);
		return query.getResultList();
	}

}